package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.entity.Sign;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.TempRecord;
import com.lanou3g.ptp.sign.service.SignService;
import com.lanou3g.ptp.utils.DateUtils;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.SignCalendarResult;
import com.lanou3g.ptp.vo.SimpleAttdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@Api(description = "签到接口")
@RestController
public class SignController {
    @Reference
    private SignService signService;

    @ApiOperation("获取某任务下某学生当天的打卡记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/task/{taskId}/signs/today")
    public PtpResult signInfosToday(@PathVariable Long taskId,
                                    @ApiIgnore @SsoStudent Student student) {

        SignCalendarResult signCalendar = signService.findSignCalendarToday(taskId, student.getId());
        return signCalendar != null ? PtpResultUtils.ok(signCalendar) : PtpResultUtils.error(500);
    }

    @ApiOperation("签到打卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/task/{taskId}/sign")
    public PtpResult sign(@PathVariable Long taskId,
                          @ApiIgnore @SsoStudent Student student,
                          @RequestBody Sign sign) {
        sign.setTaskId(taskId);
        sign.setStudentId(student.getId());
        boolean success = signService.sign(sign);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(410, "签到失败");
    }


    @ApiOperation("临时查岗打卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tempId", value = "临时查岗任务ID"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/temp-sign/{tempId}/sign")
    public PtpResult tempSign(@PathVariable Long tempId,
                              @ApiIgnore @SsoStudent Student student,
                              @RequestBody Sign sign) {
        //根据临时打卡的Id查询该数据
        boolean success = false;
            Date nowTime = new Date();
            TempRecord temp = signService.findByStartTimeAndEndTimeAndId(nowTime, tempId);
            if (temp != null) {
                sign.setTaskId(temp.getTaskId());
                sign.setStudentId(student.getId());
                success = signService.sign(sign);
            }
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(410, "签到失败");
    }


    @ApiOperation("获取某任务下某学生的所有打卡记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/task/{taskId}/signs")
    public PtpResult signInfos(@PathVariable Long taskId,
                               @ApiIgnore @SsoStudent Student student) {
        try {
            List<SignCalendarResult> signs = signService.findSignCalendar(taskId, student.getId());
            return PtpResultUtils.ok(signs);
        } catch (Exception e) {
            return PtpResultUtils.error(500);
        }
    }

    /**
     * @param taskId
     * @param studentId
     * @return
     * @author 武奇
     */
    @ApiOperation("查询某个任务下某个学生的考勤详情")
    @GetMapping("/task/{taskId}/student/{studentId}/sign")
    public PtpResult attendanceDetails(@PathVariable Long taskId, @PathVariable Long studentId) {
        List<SimpleAttdVO> details = this.signService.signDetails(taskId, studentId);
        return PtpResultUtils.ok(details);
    }
}
